package hiho.week._66;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	
	private static final int BIG_INT = 999999;
	private static final int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

	public static void main(String[] args) {
		// Inputs
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int m = in.nextInt();
		char[][] matrix = new char[n][m];
		for (int i = 0; i < n; i++) {
			String s = in.next();
			for (int j = 0; j < m; j++)
				matrix[i][j] = s.charAt(j);
		}
		
		// Calculates
		int[][] dist = new int[n][m];
		int stx = -1;
		int sty = -1;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++) {
				if (matrix[i][j] == 'H') {
					stx = i;
					sty = j;
				}
				dist[i][j] = BIG_INT;
			}
		findDists(stx, sty, dist, matrix, n, m);
		int ans = BIG_INT;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m - 1; j++)
				if (matrix[i][j] == 'S' && matrix[i][j + 1] == 'S')
					ans = Math.min(ans, dist[i][j] + dist[i][j + 1]);
		for (int i = 0; i < n - 1; i++)
			for (int j = 0; j < m; j++)
				if (matrix[i][j] == 'S' && matrix[i + 1][j] == 'S')
					ans = Math.min(ans, dist[i][j] + dist[i + 1][j]);
		
		// Outputs
		if (ans >= BIG_INT)
			System.out.println("Hi and Ho will not have lunch.");
		else
			System.out.println(ans);
	}
	
	private static void findDists(int stx, int sty, int[][] dist, char[][] matrix, int n, int m) {
		Queue<Integer> allx = new LinkedList<Integer>();
		Queue<Integer> ally = new LinkedList<Integer>();
		allx.add(stx);
		ally.add(sty);
		dist[stx][sty] = 0;
		while (!allx.isEmpty()) {
			int nowx = allx.poll();
			int nowy = ally.poll();
			int len = dist[nowx][nowy];
			for (int i = 0; i < 4; i++) {
				int nextx = nowx + dir[i][0];
				int nexty = nowy + dir[i][1];
				if (nextx < 0 || nextx >= n || nexty < 0 || nexty >= m)
					continue;
				if (matrix[nextx][nexty] == '#' || matrix[nextx][nexty] == 'P')
					continue;
				if (len + 1 >= dist[nextx][nexty])
					continue;
				dist[nextx][nexty] = len + 1;
				if (matrix[nextx][nexty] == 'S')
					continue;
				allx.add(nextx);
				ally.add(nexty);
			}
		}
	}

}
